﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HostDetector
{
    public class PartyState
    {
        List<Player> players = new List<Player>();

        public List<Player> Players
        {
            get { return players; }
        }
        bool valid = true;

        public bool Valid
        {
            get { return valid; }
        }
        public PartyState(byte[] data)
        {

            int playerPos = 16;

            while ((playerPos + 67) < data.Length)// 44 +22 +1
            {


                while ((playerPos < data.Length - 4))
                {


                    if (data[playerPos] == 1
                         && data[playerPos + 1] == 0
                         && data[playerPos + 2] == 16
                         && data[playerPos + 3] == 1)
                    {
                        if (data[playerPos + 35] == 0)
                            playerPos = playerPos - 8;
                        else
                            playerPos = playerPos - 9;

                        break;
                    }
                    else
                        playerPos++;

                }
                if (playerPos == data.Length - 4)
                    break;

                int nameStart = playerPos + 44;
                int nameLength = 0;
                while (data[nameStart + nameLength] != 0)
                    nameLength++;


                byte[] playerBytes = new byte[nameLength + 66];
                Array.Copy(data, playerPos, playerBytes, 0, nameLength + 66);
                players.Add(new Player(playerBytes));


                playerPos += (nameLength + 66);//44 +22




            }
            // int expected = ((data.Length - 150) / 71);
        }

               

    }
}
